﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Management;
using System.Text.RegularExpressions;
using ManagedWinapi.Windows;
using ManagedWinapi.Accessibility;

namespace ESLReminder.Modules
{
    public class AequitasMatchlinkHelper
    {
        public static string getMatchLinkFromCommandline()
        {
            Trace.WriteLine("Using WMI to get param info");
            try
            {
                //Use WMI to get the commandline-parameters aequitas was started with
                ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE Name='aequitas.exe'");
                ManagementObjectCollection queryCollection = query.Get();
                Debug.WriteLine(queryCollection.Count.ToString() + " aequitas instances found!");

                foreach (ManagementObject mo in queryCollection)
                {
                    //Console.WriteLine("PID [ {0} ] started '{1}' With commandline - '{2}'", mo["ProcessID"].ToString(), mo["CreationDate"].ToString(), mo["CommandLine"].ToString());
                    //if(mo["ProcessID"].ToString() == GameProcess.Id.ToString())
                    //TODO: don't print +rcon_password here
                    Trace.WriteLine(String.Format("PID [ {0} ] started '{1}' With commandline - '{2}'", mo["ProcessID"].ToString(), mo["CreationDate"].ToString(), mo["CommandLine"].ToString()));
                    Match m = Regex.Match(mo["CommandLine"].ToString(), @"-game (\S+)");
                    if (m.Success)
                    {
                        return m.Groups[1].Value;
                    }
                }
            }
            catch (Exception ex)
            {

                Trace.WriteLine("getMatchLinkFromCommandline() error: " + ex.Message);
            }
            return string.Empty;
        }

        public static string getMatchLink()
        {
            Trace.WriteLine("ESLHelpers: Entering getMatchLink()");
            string link = string.Empty;
            //#if !DEBUG
            try
            {
                //#endif
                Trace.WriteLine("Trying to get matchlink from Aequitas commandline");

                Trace.WriteLine("Trying to get matchlink from ESL Aequitas");

                ManagedWinapi.Windows.SystemWindow aeqwin = new SystemWindow(IntPtr.Zero);
                foreach (SystemWindow w in aeqwin.AllChildWindows/*.AllDescendantWindows*/)
                {
                    if (w.Title.StartsWith("ESL Aequitas"))
                    {
                        foreach (SystemWindow w2 in w.AllChildWindows)
                        {
                            Debug.WriteLine("found '" + w2.Title + "' (class '" + w2.ClassName + "')");
                            if (w2.ClassName == "ThunderRT6TextBox")
                            {
                                Debug.WriteLine("FOUND AEQUITAS-MATCHLINK!");
                                SystemAccessibleObject stopmon = SystemAccessibleObject.FromWindow(w2, AccessibleObjectID.OBJID_WINDOW);
                                foreach (SystemAccessibleObject o in stopmon.Children)
                                    if (o.RoleIndex == 42)
                                    {
                                        //Debug.WriteLine("Matchlink is: " + o.Value);
                                        //return o.Value;
                                        link = o.Value;
                                        break;
                                    }
                                break;
                            }
                        }
                        break;
                    }
                }
                //#if !DEBUG
            }
            catch (Exception ex)
            {
                Trace.WriteLine("ERROR while getting matchlink: " + ex.Message);
            }
            //#endif
            Trace.WriteLine("ESLHelpers: Leaving getMatchLink()");
            return link;
        }
    }
}
